package com.hzw.rest;

import com.hzw.api.ILoginAndRegisterService;
import com.hzw.entity.Admin;
import com.hzw.entity.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
@CrossOrigin
@RequestMapping("loginAndRegister")
public class LoginAndRegisterController {

    @Autowired
    ILoginAndRegisterService loginAndRegisterService;

    @RequestMapping("user/regist")
    @ResponseBody
    public Object registerAdmin(User user) {
        if (StringUtils.isNotEmpty(user.getUsername()) && StringUtils.isNotEmpty(user.getPassword()) && StringUtils.isNotEmpty(user.getEmail())) {
            int count = loginAndRegisterService.registerUser(user);
            if (count == 1) {
                return "success";
            } else {
                return "exist";
            }
        } else {
            return "fail";
        }
    }

    @PostMapping("user/login")
    @ResponseBody
    public Object login(@RequestBody User user) {
        if(StringUtils.isNotEmpty(user.getUsername()) && StringUtils.isNotEmpty(user.getPassword())) {
            Object res = loginAndRegisterService.loginUser(user);
            return res;
        } else {
            return "fail";
        }
    }

    @PostMapping("admin/login")
    @ResponseBody
    public Object login(@RequestBody Admin admin) {
        if (StringUtils.isNotEmpty(admin.getUsername()) && StringUtils.isNotEmpty(admin.getPassword())) {
            Object res = loginAndRegisterService.loginAdmin(admin);
            return res;
        } else {
            return "fail";
        }
    }

}
